//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     会员扩展业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Enum.Common;
using CL.Enum.Common.Type;
using CL.Game.DAL;
using CL.Game.Entity;
using CL.Json.Entity;
using CL.Redis.BLL;
using CL.Tools.Common;
using System;

namespace CL.Game.BLL
{

    /// <summary>
    /// UsersExtendBLL 业务
    /// </summary>
    public class UsersExtendBLL
    {
        Log log = new Log("UsersExtendBLL");
        UsersExtendDAL dal = new UsersExtendDAL(DbConnectionEnum.CaileGame);

        /// <summary>
        /// 根据用户编号查询用户衍生对象
        /// </summary>
        /// <param name="UserCode">用户编号</param>
        /// <returns></returns>
        public UsersExtendEntity QueryEntityByUserCode(long UserCode)
        {
            return dal.QueryEntityByUserCode(UserCode);
        }

        /// <summary>
        /// 集成登陆查询
        /// </summary>
        /// <param name="IntegrateType">集成登录类型</param>
        /// <param name="IntegrateToken">集成登陆Token</param>
        /// <returns></returns>
        public UsersExtendEntity QueryEntityByUserCode(short IntegrateType, string IntegrateToken)
        {
            return dal.QueryEntityByUserCode(IntegrateType, IntegrateToken);
        }
        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertInfo(UsersExtendEntity entity)
        {
            return dal.InsertInfo(entity);
        }
        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int ModifyEntity(UsersExtendEntity entity)
        {
            return dal.ModifyEntity(entity);
        }
        #region 自定义方法

        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="UserCode"></param>
        /// <param name="IntegratedOpenID"></param>
        /// <param name="IntegratedToken"></param>
        /// <param name="LoginType"></param>
        /// <param name="SourceType"></param>
        /// <param name="NickName"></param>
        /// <param name="HeadImgUrl"></param>
        /// <returns></returns>
        public UsersExtendEntity InsertIntegrated(long UserCode, string IntegratedOpenID, string IntegratedToken, short LoginType, short SourceType, string NickName, string HeadImgUrl, string Uid = null)
        {
            UsersExtendEntity Entity = new UsersExtendEntity()
            {
                AvatarAddress = HeadImgUrl,
                BindTime = DateTime.Now,
                BindType = (byte)(LoginType == 1 ? 3 : LoginType == 2 ? 2 : LoginType == 3 ? 4 : 0),
                Email = " ",
                CreateTime = DateTime.Now,
                IpAddress = " ",
                IsBindTel = false,
                IsVerify = false,
                NickName = NickName,
                SourceType = (byte)SourceType,
                SpecialLevel = 0,
                UserID = UserCode,
                UserLevel = 0,
                RelationID = Uid == null ? "" : Uid,
                FullName = " ",
                IDNumber = " ",
                Idols = 0,
                AliPayID = " ",
                AliPayToken = " ",
                QQID = " ",
                QQToken = " ",
                WechatID = " ",
                WechatToken = " "
            };
            switch (LoginType)
            {
                case 1:
                    Entity.QQID = IntegratedOpenID;
                    Entity.QQToken = IntegratedToken;
                    break;
                case 2:
                    Entity.WechatID = IntegratedOpenID;
                    Entity.WechatToken = IntegratedToken;
                    break;
                case 3:
                    Entity.AliPayID = IntegratedOpenID;
                    Entity.AliPayToken = IntegratedToken;
                    break;
            }
            if (this.InsertInfo(Entity))
                return Entity;
            else
                return null;
        }

        /// <summary>
        /// 校验身份证
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <param name="FullName"></param>
        /// <param name="IDCardNo"></param>
        /// <param name="VerifyCode"></param>
        /// <returns></returns>
        public JsonResult IDVerification(string Token, long UserCode, string FullName, string IDCardNo, string VerifyCode, string Mobile)
        {
            JsonResult result = null;
            try
            {
                var SignInEntity = new SystemRedis().SignInByUserCodeRedis(UserCode);
                if (!string.IsNullOrEmpty(SignInEntity.Mobie))
                {
                    if (string.IsNullOrEmpty(Mobile.Trim()))
                        Mobile = SignInEntity.Mobie;
                    else
                    {

                        if (Mobile.Trim() != SignInEntity.Mobie)
                        {
                            return new JsonResult()
                            {
                                Code = (int)ResultCode.MobileFailure,
                                Msg = Common.GetDescription(ResultCode.MobileFailure)
                            };
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(Mobile.Trim()))
                        return new JsonResult()
                        {
                            Code = (int)ResultCode.MobileFormatError,
                            Msg = Common.GetDescription(ResultCode.MobileFormatError)
                        };
                    int VerifyRec = new SystemRedis().VerifyCodeRedis(Mobile, VerifyCode, (int)VerifyTypeEnum.Identity);
                    if (VerifyRec == 0)
                    {
                        //绑定手机号码
                        var UsersEntity = new UsersBLL().QueryEntityByUserMobile(Mobile);
                        if (UsersEntity == null || UsersEntity.UserID == UserCode)
                        {
                            #region 更新DB
                            UsersEntity = new UsersBLL().QueryEntityByUserCode(UserCode);
                            UsersEntity.UserMobile = Mobile;
                            new UsersBLL().ModifyEntity(UsersEntity);
                            #endregion
                            #region 更新Redis
                            SignInEntity = new SystemRedis().SignInByUserCodeRedis(UserCode);
                            SignInEntity.Mobie = Mobile;
                            new SystemRedis().ModifySignInSessionRedis(SignInEntity);
                            #endregion
                        }
                        else
                            return new JsonResult()
                            {
                                Code = (int)ResultCode.Mobiles,
                                Msg = Common.GetDescription(ResultCode.Mobiles)
                            };
                    }
                }
                int VerifyResult = new SystemRedis().VerifyCodeRedis(Mobile, VerifyCode, (int)VerifyTypeEnum.Identity);
                if (VerifyResult == (int)ResultCode.Success)
                {
                    #region Redis更新
                    SignInEntity.IsCertification = true;
                    SignInEntity.FullName = FullName;
                    new SystemRedis().ModifySignInSessionRedis(SignInEntity);
                    #endregion
                    #region DB更新
                    var ExtendEntity = this.QueryEntityByUserCode(UserCode);
                    if (ExtendEntity != null)
                    {
                        ExtendEntity.FullName = FullName;
                        ExtendEntity.IDNumber = IDCardNo;
                        ExtendEntity.IsVerify = true;
                        this.ModifyEntity(ExtendEntity);
                    }
                    #endregion
                    //实名认证成功解除禁言
                    new IM.Communication().UnBanToPost(UserCode, Token, "通过实名认证解除禁言");
                    result = new JsonResult()
                    {
                        Code = (int)ResultCode.Success,
                        Msg = Common.GetDescription(ResultCode.Success)
                    };

                }
                else if (VerifyResult == 1)
                    result = new JsonResult()
                    {
                        Code = (int)ResultCode.CodeError,
                        Msg = Common.GetDescription(ResultCode.CodeError)
                    };
                else
                    result = new JsonResult()
                    {
                        Code = (int)ResultCode.CodeInvalidation,
                        Msg = Common.GetDescription(ResultCode.CodeInvalidation)
                    };
            }
            catch (Exception ex)
            {
                log.Write("校验身份证错误[IDVerification]：" + ex.StackTrace, true);
                result = new JsonResult()
                {
                    Code = (int)ResultCode.SystemBusy,
                    Msg = Common.GetDescription(ResultCode.SystemBusy)
                };
            }
            return result;
        }
        #endregion
    }
}
